package com.newsnetwork.article;

import com.newsnetwork.api.config.RabbitMQDelayConfig;
import com.newsnetwork.article.service.ArticleService;
import freemarker.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author DingNing
 * @date 2022/11/21 15:21
 * @Description:
 */
@Component
public class RabbitMQDelayConsumer {
    final static Logger logger = LoggerFactory.getLogger(RabbitMQDelayConsumer.class);
    @Autowired
    private ArticleService articleService;

    @RabbitListener(queues = {RabbitMQDelayConfig.QUEUE_DELAY})
    public void watchQueue(String payload, Message message) {

        // 消费者接收到定时发布的延迟消息，修改当前的文章状态为`即时发布`
        String articleId = payload;
        logger.info("定时文章发布"+articleId);
        articleService.updateArticleToPublish(articleId);
        // 文章发布时间到生成HTML静态页面
        try {
            articleService.updateArticleToGridFS(articleId);
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        }
    }

}
